package com.nf.servlet;

import com.google.gson.Gson;
import com.nf.dao.StudentDao;
import com.nf.dao.StudentsDaoImpl;
import com.nf.entity.Students;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/UpdateStu")
public class UpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入updateServlet");
        //获取客户端传递过来的参数
        int id =Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        int age =Integer.parseInt(request.getParameter("age"));
        //将客户端传过来的String转换为Date类型，需要捕获异常
        Date birthday = null;
        try {
            System.out.println(request.getParameter("birthday"));
            birthday = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("birthday"));
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("后端时间转换问题");
        }
        Students student = new Students(id,name,sex,age,birthday);
        //调用dao层的修改方法
        StudentDao dao = new StudentsDaoImpl();
        int result = dao.update(student);
        System.out.println(result);
        //重定向回客户端页面
        response.sendRedirect("index.html");
    }
}
